package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.CompanyDynamic;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.CompanyDynamicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 公司动态 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-05-30
 */
@RestController
@RequestMapping("/sys/dynamic")
@Api(description = "公司动态管理")
public class CompanyDynamicController extends BaseController<CompanyDynamicService, CompanyDynamic> {


    @ApiOperation("获取全部公司动态分页")
    @IgnoreAuth
    @RequestMapping(value = "/getDynamicPage", method = RequestMethod.POST)
    public ReturnObject<List<CompanyDynamic>> getAllNewPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                            @ApiParam("商铺id") @RequestParam Long storeId) {

        EntityWrapper<CompanyDynamic> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("store_id", storeId);
        ew.orderBy("crt_time", false);
        Page<CompanyDynamic> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<CompanyDynamic> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "新增成功", baseService.selectById(id));
    }

    @ApiOperation("新增/修改（新增时：id为空）")
    @RequestMapping(value = "/saveNewInfo", method = RequestMethod.POST)
    public ReturnObject<CompanyDynamic> saveNewInfo(@ApiParam("主题") @RequestParam String title,
                                                    @ApiParam("封面") @RequestParam String dynamicUrl,
                                                    @ApiParam("内容") @RequestParam String content,
                                                    @ApiParam("商铺id") @RequestParam Long storeId,
                                                    @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {

        if (id != 0) {
            CompanyDynamic info = baseService.selectById(id);
            info.setTitle(title);
            info.setDynamicUrl(dynamicUrl);
            info.setContent(content);
            baseService.updateById(info);
        } else {
            CompanyDynamic info = new CompanyDynamic();
            info.setTitle(title);
            info.setDynamicUrl(dynamicUrl);
            info.setContent(content);
            info.setStoreId(storeId);
            baseService.insert(info);
        }

        return new ReturnObject<>(0, "新增成功", null);
    }



    @ApiOperation("删除")
    @RequestMapping(value = "/delNewById", method = RequestMethod.POST)
    public ReturnObject<CompanyDynamic> delNewById(@ApiParam("id") @RequestParam Long id) {

        CompanyDynamic info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }

}

